//其实页面可以和movie-order页面复用
const app = getApp();
Page({
  data: {
    orderList: [], //全部订单
    pageNum: 0,
    pageSize: 10,
    has: true,
    loading: false,
    swiperViewHeight: 0,
  },
  onLoad() {
    this.initData()
  },
  initData() {
    var that = this;
    // 粉丝统计
    wx.request({
      url: app.globalData.url + '/orderManage/queryFansOrderStatistics',
      method: "GET",
      header:{
        consumerId: app.globalData.userInfo.consumerId
      },
      success(res) {
        if (res.data.code == 200) {
          that.setData({
            order: res.data.data,
          })
        }
      }
    })
   this.getList();
  },
  getList(){
    var that = this;

     // 粉丝订单列表
    if (that.data.loading) return
  
    that.data.loading = true;
    var pageNum = that.data.pageNum;
    var pageSize = that.data.pageSize;
    wx.request({
      url: app.globalData.url + '/orderManage/queryFansOrders',
      method: 'POST',
      data: {
        pageNum: ++pageNum,
        pageSize
      },
      header: {
        'Accept': 'application/json',
        'consumerId': app.globalData.userInfo.consumerId
      },
      success: function (res) {
        if (res.data.code == 200) { 
          const orderList = that.data.orderList.concat(res.data.rows && res.data.rows.map(i=> {
            return {
              ...i,
              status: that.typeConvert(i.payFlag, 'color'),
              statusName: that.typeConvert(i.payFlag, 'name')
            }
          }));
          that.setData({
            has: pageNum <= Math.ceil(res.data.total / 10),
            orderList,
            pageNum
          })
          that.setSwiperHeight();
          that.data.loading = false;
        }
      }
    })
  },
  setSwiperHeight: function () {
    var query = wx.createSelectorQuery();
    //选择id
    var that = this;
    query.selectAll('.swiperH').boundingClientRect(function (rect) {
      console.log(rect[0].height)
      var itemsLength = 0;
      itemsLength = (rect[0].height + 18)*that.data.orderList.length + 18;
      that.setData({
        swiperViewHeight: itemsLength
      })
    }).exec();
  },
  onReachBottom: function () {
    if (this.data.has)
      this.getList();
  },
  typeConvert: function(val, name){
    let status = {
      NO_PAY: '未支付',
      PAY_ING: '支付中',
      PAY_FAIL: '支付失败',
      PAY_SUCCESS: '已支付',
      SENDING: '退款发送中',
      PROCESSING: '退款中',
      SUCCESS: '退款成功',
      CLOSED: '退款关闭',
      ABNORMAL: '退款异常',
    }
    let color = {
      NO_PAY: 'color1',
      PAY_ING: 'color1',
      PAY_FAIL: 'color0',
      PAY_SUCCESS: 'color2',
      SENDING: 'color1',
      PROCESSING: 'color1',
      SUCCESS: 'color2',
      CLOSED: 'color1',
      ABNORMAL: 'color0',
    }
    if (name == 'name') {
      return status[val]
    } else if (name == 'color') {
      return color[val]
    }
  },
})